<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="layout" content="bocMain"/>
    <title>进件作业系统</title>
</head>

<body>
%{--<header class="header-title">运营商验证</header>--}%

<div class="step1">
    <g:form action="creditAuthorizeSave" class="myform" method="post">
        <input type="hidden" name="id" id="id" value="${this?.opportunity?.id}">
        <input type="hidden" name="type" id="type" value="${this?.type}">

        <div class="weui-cells__title">请先阅读并同意个人信用信息查询及报送授权书</div>
        <div class="weui-cells weui-cells_checkbox checkbox-group">
            <label for="checkbox1" class="weui-agree">
                <g:checkBox name="checkbox1" value="${false}" id="checkbox1"  class="weui-agree__checkbox" style="border-radius: 50%"/>
                <span class="weui-agree__text">
                我已阅读并同意签署<a href="/bocAppOpportunity/certificateAuthorization">《个人信用信息查询及报送授权书》</a>
                </span>
            </label>
        </div>

        <div class="weui-cells weui-cells_form">
            <div class="weui-cells__title">借款人手机号码</div>
            <div class="weui-cell" style="margin-bottom: 3.5rem;border-width: 0 0 1px 0;border-radius: 0">
                <div class="weui-cell__bd">
                    <g:textField class="weui_input" name="cellphone" id="cellphone" value="${this?.contact?.cellphone}"/>
                </div>
            </div>

            <div class="weui-cells__title">短信验证码</div>
            <div class="weui-cell" style="border-width: 0 0 1px 0;border-radius: 0">
                <div class="weui-cell__bd">
                    <input class="weui_input" type="number" placeholder="请输入验证码" name="verifiedCode" id="verifiedCode"/>
                </div>
                <div class="weui-cell__ft" style="margin-right: 4px">
                    <input id="btnSendVerifiedCode" type="button" class="vCode-btn weui-btn weui-btn_mini weui-btn_primary" value="获取验证码"/>
                </div>
            </div>
        </div>
    </g:form>
    <div class="bigBtn">
        <button type="button" class="weui-btn weui-btn_primary" id="verificationbtn">下一步</button>
    </div>
</div>

<script>
    $(function ()
    {
        var time = 30;
        var interval;
        $('#btnSendVerifiedCode').click(function () 
        {
            var sendMessageUrl = "/bocAppOpportunity/sendVerifiedCodeForAuthorize"
            var type = $("#type").val();
            if (type == "spouse")
            {
                sendMessageUrl = "/bocAppOpportunity/sendVerifiedCodeForSpouseAuthorize"
            }

            if ($("#cellphone").val().length == 0) {
                $(".helpMsg").text("请输入手机号").fadeIn(200);
                $('#btnSendVerifiedCode').addClass("vCodeActive").attr('disabled', 'disabled');
                setTimeout(function () {
                    $(".helpMsg").fadeOut(200);
                    $('#btnSendVerifiedCode').removeClass("vCodeActive").removeAttr('disabled');
                }, 2000);
                return;
            }
            if (/^1\d{10}$/.test($("#cellphone").val())) {
                $.ajax({
                    type: "POST",
                    url: sendMessageUrl,
                    data: {
                        cellphone: $("#cellphone").val(),
                        id: $("#id").val()
                    },
                    beforeSend: function () {
                        interval = setInterval(function () {
                            if (time > 0) {
                                $('#btnSendVerifiedCode').attr('disabled', 'disabled').addClass("vCodeActive");
                                $('#btnSendVerifiedCode').val("" + (time--) + '秒后重试');
                            } else {
                                $('#btnSendVerifiedCode').removeAttr('disabled').removeClass("vCodeActive");
                                $('#btnSendVerifiedCode').val("获取验证码");
                                time = 60;
                                clearInterval(interval);
                            }
                        }, 1000);

                    },
                    success: function (data) {
                        if (data.status == "success") {
                            helpMessage("验证码发送成功");
                        }
                        if (data.status == "error") {
                            $(".helpMsg").text(data.errorMessage).fadeIn(200);
                            setTimeout(function () {
                                $(".helpMsg").fadeOut(200);
                            }, 2000);
                        }
                    }
                });
            } else {
                helpMessage("请输入正确的手机号");
            }
        });

        setTimeout(function () {
            $(".message").fadeOut(200);
        }, 2000);

        function helpMessage(message) {
            $(".helpMsg").text(message).fadeIn(200);
            setTimeout(function () {
                $(".helpMsg").fadeOut(200);
            }, 2000);
        }

        $("#verificationbtn").click(function () {
            // 参数合法性校验
            if(!$('#checkbox1').is(':checked')) {
                helpMessage("请同意个人信用信息查询及报送授权书");
                return;
            }
            var cellphoneStr = $("#cellphone").val().trim();
            var verifiedCode = $("#verifiedCode").val().trim();
            if (cellphoneStr.length == 0) {
                helpMessage("请输入手机号");
                return;
            }
            if (!(/^1\d{10}$/.test(cellphoneStr))) {
                helpMessage("请输入正确的手机号");
                return;
            }
            if (!verifiedCode) {
                helpMessage("请输入验证码");
                return;
            }
            var type = $("#type").val();
            var contractType = "";
            if(type == "spouse"){
                contractType = "配偶征信授权书"
            }else {
                contractType = "征信授权书"
            }
            $.ajax({
                method: "POST",
                url: "/bocAppOpportunity/anxinqianEvaluateComponent",
                data: {
                    component:"安心签创建合同3201",
                    contractType:contractType,
                    opportunityId:$("#id").val(),
                    type:type
                },
                success: function (data) {
                    if (data.code == "200")
                    {
                        console.log("合同创建成功");
                        $(".myform").submit();
                    }
                    else
                    {
                        helpMessage("合同生成失败，请联系管理员");
                        return;
                    }
                },
                error: function (jqXHR) {
                    console.log(JSON.stringify(jqXHR));
                }
            });

        });

        var message = '${flash.message}';
        if (message) {
            $(".helpMsg").text(message).fadeIn(200);
            setTimeout(function () {
                $(".helpMsg").fadeOut(200);
            }, 2000);
        }
    })

</script>

</body>
</html>